package xyz.labradormm.auth.util;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author hhj
 * @description
 * @date 2023-07-20 15:30
 * @menu
 */
public class CodeAutoGenerator {


    public static void main(String[] args) {
        // 加载 YAML 配置
        JSONObject yamlConfig = YamlConfigLoader.loadYamlConfig();
        JSONObject datasource = yamlConfig.getJSONObject("spring").getJSONObject("datasource");
        String author="huanghaojian";
        List<String> tables = Arrays.asList(
                "sys_user");
        String url=datasource.getString("url");
        String username=datasource.getString("username");
        String password=datasource.getString("password");
        // 获取当前程序的工作目录
        String projectDir = Paths.get("").toAbsolutePath().toString();
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> {
                    builder.author(author) // 设置作者
                            .enableSpringdoc()
                            .outputDir(projectDir+"\\src\\main\\java"); // 指定输出目录
                })
                .dataSourceConfig(builder -> builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                    int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                    if (typeCode == Types.SMALLINT) {
                        // 自定义类型转换
                        return DbColumnType.INTEGER;
                    }
                    return typeRegistry.getColumnType(metaInfo);

                }))
                .packageConfig(builder -> {
                    builder.parent("xyz.labradormm.auth") // 设置父包名
                            .entity("domain.po")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectDir+"\\src\\main\\resources\\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.entityBuilder()
                            .enableTableFieldAnnotation()
                            .enableFileOverride() //开启entity文件覆盖
                            .enableLombok();
                    builder.controllerBuilder()
//                            .enableFileOverride() //开启controller文件覆盖
                            .enableRestStyle();
//                    builder.serviceBuilder().enableFileOverride(); //开启service文件覆盖
                    builder.mapperBuilder()
//                            .enableFileOverride() //开启mapper文件覆盖
                            .enableBaseColumnList();
                    builder.addInclude(tables); // 设置需要生成的表名
                })
                .templateConfig(builder -> {
                    builder.entity("/templates/generator/entity.java");
                    builder.controller("/templates/generator/controller.java"); //自定义模板
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }


}
